using System.Collections.Generic;

public static class BulletCountSaver
{
	private static Dictionary<string, InGameAmmoCount> ammoCounts;

	public static void OnChangeWeapon(BaseWeapon weapon)
	{
		if (ammoCounts == null)
		{
			ammoCounts = new Dictionary<string, InGameAmmoCount>();
		}
		if (!ammoCounts.ContainsKey(weapon.name))
		{
			InGameAmmoCount inGameAmmoCount = new InGameAmmoCount();
			if (!weapon.SaveBullet)
			{
				inGameAmmoCount.ammo = weapon.addCountBulletBonuse - weapon.maxChargerAmmo;
				inGameAmmoCount.chargerAmmo = weapon.maxChargerAmmo;
			}
			else
			{
				inGameAmmoCount.ammo = settings.GetConsumableCount(weapon.name);
			}
			inGameAmmoCount.saveBullet = weapon.SaveBullet;
			ammoCounts[weapon.name] = inGameAmmoCount;
		}
		else
		{
			SetStartAmmo(weapon);
		}
	}

	public static void SetStartAmmo(BaseWeapon weapon)
	{
		if (!weapon.SaveBullet)
		{
			if (weapon.maxChargerAmmo == 1)
			{
				if (weapon.ammo < weapon.addCountBulletBonuse)
				{
					weapon.ammo = weapon.addCountBulletBonuse;
				}
			}
			else if (weapon.AllBullet < weapon.maxChargerAmmo * 2)
			{
				weapon.ammo = weapon.addCountBulletBonuse - weapon.maxChargerAmmo;
			}
			weapon.chargerAmmo = weapon.maxChargerAmmo;
		}
		else
		{
			weapon.ammo = settings.GetConsumableCount(weapon.name);
			weapon.chargerAmmo = 0;
		}
	}

	public static int GetAmmo(string name)
	{
		if (ammoCounts == null || !ammoCounts.ContainsKey(name))
		{
			return 0;
		}
		return ammoCounts[name].ammo;
	}

	public static int GetChargerAmmo(string name)
	{
		if (ammoCounts == null || !ammoCounts.ContainsKey(name))
		{
			return 0;
		}
		return ammoCounts[name].chargerAmmo;
	}

	public static void MinusOneConsumble(string name)
	{
		if (ammoCounts[name].saveBullet)
		{
			settings.SetConsumableCount(name, AllAmmo(name) - 1);
		}
	}

	public static void SetAmmo(string name, int count)
	{
		if (ammoCounts != null && ammoCounts.ContainsKey(name))
		{
			ammoCounts[name].ammo = count;
		}
	}

	public static void SetChargerAmmo(string name, int count)
	{
		if (ammoCounts != null && ammoCounts.ContainsKey(name))
		{
			ammoCounts[name].chargerAmmo = count;
		}
	}

	public static int AllAmmo(string name)
	{
		if (ammoCounts == null || !ammoCounts.ContainsKey(name))
		{
			return 0;
		}
		return (int)ammoCounts[name].ammo + (int)ammoCounts[name].chargerAmmo;
	}

	public static void SaveConsumble(string name)
	{
		if (ammoCounts != null && settings.GetConsumableCount(name) != AllAmmo(name))
		{
			settings.SetConsumableCount(name, AllAmmo(name));
		}
	}

	public static void Destroy()
	{
		ammoCounts.Clear();
		ammoCounts = null;
	}
}
